package com.dinglian.ots.controller.common;

import com.alibaba.fastjson.JSONObject;
import com.dinglian.ots.common.HttpRequest;
import com.dinglian.ots.common.ToolUtils;
import com.dinglian.ots.common.model.Constant;
import com.dinglian.ots.dao.model.User;
import com.dinglian.ots.exception.GlobalException;
import com.dinglian.ots.result.CodeMsg;
import com.dinglian.ots.result.Result;
import com.dinglian.ots.service.BrandService;
import com.dinglian.ots.service.UserService;
import com.dinglian.ots.shiro.MyShiroToken;
import com.dinglian.ots.vo.user.BrandLoginVo;
import com.dinglian.ots.vo.user.LoginByPasswordVo;
import com.dinglian.ots.vo.user.LoginVo;
import com.dinglian.ots.vo.user.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author wangbo
 */
@Slf4j
@RestController
@Validated
@Api(value = "/user", tags = "用户模块")
@RequestMapping("/user/")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private BrandService brandService;

    @ApiOperation(value = "买手密码登录")
    @PostMapping("buyerLogin")
    public Result buyerLogin(@RequestBody @Validated LoginByPasswordVo loginByPasswordVo) {
        UserVo userVo = new UserVo();
        try {
            Subject subject = SecurityUtils.getSubject();
            MyShiroToken token = new MyShiroToken(loginByPasswordVo.getUsername(), loginByPasswordVo.getPassword(), Constant.BUYER_ROLE);
            subject.login(token);
            userVo = (UserVo) subject.getPrincipal();
            String roleName = userVo.getRoleName();
            if (Constant.BRAND_ROLE_NAME.equals(roleName)||Constant.SHOWROOM_ROLE_NAME.equals(roleName)){
                return  Result.error(CodeMsg.TO_BRAND_LOGIN);
            }else if (Constant.ADMIN_ROLE_NAME.equals(roleName)){
                return  Result.error(CodeMsg.TO_ADMIN_LOGIN);
            }
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        }catch (UnknownAccountException e) {
            return Result.error(CodeMsg.USERNAME_IS_NOT_EXIST);
        } catch (IncorrectCredentialsException e) {
            return Result.error(CodeMsg.PASSWORD_ERROR);
        } catch (AuthenticationException e) {
            return Result.error(CodeMsg.PASSWORD_ERROR);
        }
        return Result.success(userVo);
    }


    @ApiOperation(value = "品牌登录")
    @PostMapping("brandLogin")
    public Result brandLogin(@RequestBody @Validated BrandLoginVo brandLoginVo) {
        UserVo userVo = new UserVo();
        try {
            userVo = userService.brandLogin(brandLoginVo);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        }catch (UnknownAccountException e) {
            return Result.error(CodeMsg.MOBILE_IS_NOT_EXIST);
        } catch (IncorrectCredentialsException e) {
            return Result.error(CodeMsg.CODE_ERROR);
        }  catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.success(userVo);
    }


    @ApiOperation(value = "退出登录")
    @PostMapping("logout")
    public Result logout() {
        userService.logout();
        return Result.successNoData();
    }

    @ApiOperation(value = "用于再次获取用户信息")
    @GetMapping("getUserInfoAgain")
    public Result getUserInfoAgain(@RequestParam("sessionToken") String sessionToken) {
        UserVo userVo = new UserVo();
        try {
            userVo = userService.getUserInfoAgain(sessionToken);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.success(userVo);
    }
}
